﻿using System;
using System.Diagnostics;
using Ninject.Core.Activation;
using Ninject.Core.Creation;
using IronCow;
using IronCow.Rest;

namespace Milkify
{
    internal class RtmProvider : SimpleProvider<Rtm>
    {
        private const string sApiKey = "5d426d1fcac9949929da5acb9615eee5";
        private const string sSharedSecret = "b4c9e5e5b98996c6";

        protected override Rtm CreateInstance(IContext context)
        {
            Rtm.Dispatcher = App.Current.Dispatcher;

            RestClient client = new RestClient(sApiKey, sSharedSecret);
            Rtm rtm = new Rtm(client);
            rtm.Client.Timeout = 10000;
            rtm.Client.Cache = new IronCow.Cache.MemoryResponseCache();
            rtm.Client.Cache.Validity = new TimeSpan(1, 0, 0);

            if (Milkify.Properties.Settings.Default.UseProxy)
            {
                string address = Milkify.Properties.Settings.Default.ProxyAddress;
                rtm.Client.Proxy = new System.Net.WebProxy(address);
            }

            ToggleDebugBehaviours(rtm);

            return rtm;
        }

        [Conditional("RTM_LOCAL")]
        private static void ToggleDebugBehaviours(Rtm rtm)
        {
            string commandLineArguments = Process.GetCurrentProcess().StartInfo.Arguments;

            rtm.DisableSyncing();
            var names = new string[] { "Inbox", "Sent", "p-nldd", "p-vancouver", "p-.net", "Home", "Urgent", "Shared with Laure", "Someday", "Work", "Ghouls" };
            foreach (var name in names)
            {
                var taskList = new TaskList(name);
                rtm.TaskLists.Add(taskList);
                taskList.AddTask(new Task("Do something") { Due = "Today", Tags = { "test", "blah" } });
                taskList.AddTask(new Task("Do something else") { Due = "Tomorrow", Tags = { "blah", "bleh", "todo" } });
                taskList.AddTask(new Task("Think about that") { Tags = { "at-home", "at-work", "try" } });
                taskList.AddTask(new Task("And a task name that is so long it should wrap around") { Tags = { "at-home", "at-work", "try" } });
                taskList.AddTask(new Task("Very important!") { Due = "Today", Priority = TaskPriority.One, Tags = { "todo" } });
                taskList.AddTask(new Task("Somewhat important") { Due = "Tomorrow", Priority = TaskPriority.Two, Tags = { "todo", "at-home" } });
                taskList.AddTask(new Task("Kinda important but not too much") { Due = "2 days", Priority = TaskPriority.Three, Tags = { "test", "blah" } });
                taskList.AddTask(new Task("Something every week") { Recurrence = "every week" });
                taskList.AddTask(new Task("Something every week on monday") { Recurrence = "every monday", Due = "Monday" });
            }
        }
    }
}
